<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Weather extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("weather_model");
    }

    public function index(){
        $this->load->view('weather');
    }

    public function upload() {
        $this->load->view('upimage');
    }

    //图片上传
    public function uploadfile() {
        $config['upload_path'] = getcwd().'/skimImg/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 2048;
        $config['max_width'] = 0;
        $config['max_height'] = 0;
        $config['remove_spaces'] = true;
        $config['file_name'] = md5(time());
        $this->load->library('upload', $config);
        $info = $this->upload->do_upload('upimage');//editormd-image-file
        //返回参数
        $success = 0;
        $message = "失败";
        $url = '';
        if ($info) {
            $str = $this->upload->data();
            $formatName = $str['file_name'];
            $imgDir =  'skimImg/'.$formatName;
            $imgName = $str['client_name'];
            $imgWidth = $str['image_width'];
            $imgHeight = $str['image_height'];

            $imgData = array(
                'typeid'=> 1,
                'imgstr'=> $imgDir,
                'imgname'=> $imgName,
                'width'=> $imgWidth,
                'height'=> $imgHeight
            );
            $result = $this->saveImage($imgData);
            if ($result) {
                $success = 1;
                $message = "成功";
            } else {
                //写入失败， 同时删除云端及 本地图片
            }
        }
        $data = array(
            'success'=>$success,
            'message'=>$message,
            'url'=> $url
        );
        echo json_encode($data);
    }

    //插入新的图片
    public function saveImage($data) {
        $result = $this->weather_model->insertImage($data);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}